package com.td.erhome.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.models.ArticleBean;
import com.td.erhome.ui.article.ArticleContentActivity;
import com.td.erhome.utils.DisplayUtil;
import com.td.erhome.utils.ImageLoader;

import java.util.List;


/**
 * Created by Administrator on 2017/4/18 0018.
 */

public class HouseMsgListAdapter extends RecyclerView.Adapter<HouseMsgListAdapter.HouseHolder> {

    private final DisplayMetrics displayMetrics;
    private final float density;
    private Activity mContext;
    public List<ArticleBean.ArticleListBean> mArticle_list;
    private int mType;

    public HouseMsgListAdapter(Activity context, List<ArticleBean.ArticleListBean> house_list) {
        mContext = context;
        this.mArticle_list = house_list;

        //获取当前手机屏幕的像素密度比例
        displayMetrics = context.getResources().getDisplayMetrics();
        density = displayMetrics.density;
    }

    public void addData(List<ArticleBean.ArticleListBean> house_list){
        mArticle_list.addAll(house_list);
        notifyDataSetChanged();
    }

    public void clearData(){
        mArticle_list.clear();
        notifyDataSetChanged();
    }

    @Override
    public HouseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_house_msg_item, null);
        HouseHolder cardHolder = new HouseHolder(inflate);
        return  cardHolder;
    }
    @Override
    public void onBindViewHolder(final HouseHolder holder, final int position) {
        ImageLoader.loadImage(mContext,mArticle_list.get(position).cover_img,R.mipmap.td_moreng,holder.imgBanner);
        holder.mTvAddress.setText(mArticle_list.get(position).article_name);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ArticleContentActivity.class);
                intent.putExtra("url",mArticle_list.get(position).content_url);
                intent.putExtra("title","房产政策");
                mContext.startActivity(intent);

            }
        });
        holder.mLinMark.removeAllViews();
        addFacilitiesImg(holder.mLinMark,mArticle_list.get(position).article_tag);
    }

    private void addFacilitiesImg(LinearLayout linearLayout , List<String> house_tag) {
        for (int i=0 ; i<house_tag.size() ; i++){
            String s = house_tag.get(i);
            TextView textView = new TextView(mContext);

            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            //设置居中显示：
            layoutParams.gravity = Gravity.CENTER;
            //设置它的上下左右的margin
            layoutParams.setMargins((int)(4* density) , 0 , (int)(4* density) , 0);

            textView.setLayoutParams(layoutParams);
            textView.setTextColor(mContext.getResources().getColor(R.color.color_048d99));
            textView.setBackgroundResource(R.drawable.shape_status);
            textView.setPadding((int)(5* density) , (int)(3*density) , (int)(5* density) , (int)(3*density));
            textView.setText(s);
            textView.setTextSize(DisplayUtil.px2sp(mContext,34));
            linearLayout.addView(textView);
        }

        //   linearLayout.setPadding(0 , (int)(10* density) , 0 , (int)(10* density));
    }


    @Override
    public int getItemCount() {
        return mArticle_list.size();
    }

    public class HouseHolder extends RecyclerView.ViewHolder {

        private final ImageView imgBanner;
        private final TextView mTvAddress;
        private final LinearLayout mLinMark;


        public HouseHolder(View itemView) {
            super(itemView);
            imgBanner = (ImageView)itemView.findViewById(R.id.img_banner);
            mTvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            mLinMark = (LinearLayout) itemView.findViewById(R.id.lin_mark);

        }
    }
}
